package com.yuuzy.view;

import com.yuuzy.dao.Buy_FundDAO;
import com.yuuzy.dao.ClientDAO;
import com.yuuzy.dao.FundDAO;
import com.yuuzy.model.Buy_Fund;
import com.yuuzy.model.Client;
import com.yuuzy.model.Fund;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

public class BuyFund extends JInternalFrame {

    private FundDAO fundDAO = new FundDAO();
    private Buy_FundDAO buy_fundDAO = new Buy_FundDAO();


    private JTextField BI_nameText;
    private JTable fund_table;
    private JTextField fund_idTxt;
    private JTextField fund_typeTxt;
    private JTextField fund_managerTxt;
    private JTextField fund_nameTxt;
    private JTextField fund_risklevelTxt;
    private JTextField fund_amountTxt;
    private JTextField client_nameTxt;
    private JTextField client_passwordTxt;
    private JTextField fundqTxt;

    /**
     * Create the frame.
     */
    public BuyFund() {
        setIconifiable(true);
        setClosable(true);
        setTitle("购买基金");
        setBounds(100, 100, 982, 963);

        JLabel lblNewLabel = new JLabel("购买基金：");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));

        BI_nameText = new JTextField();
        BI_nameText.setColumns(10);

        JScrollPane scrollPane = new JScrollPane();

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u8D2D\u4E70\u57FA\u91D1\u4EA7\u54C1", TitledBorder.LEADING, TitledBorder.TOP, null, SystemColor.textHighlight));

        JButton button = new JButton("查询 ");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                queryActionPerformed(e);
            }
        });
        button.setFont(new Font("宋体", Font.PLAIN, 20));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(191)
                                                .addComponent(lblNewLabel)
                                                .addGap(36)
                                                .addComponent(BI_nameText, GroupLayout.PREFERRED_SIZE, 194, GroupLayout.PREFERRED_SIZE)
                                                .addGap(85)
                                                .addComponent(button, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(58)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(panel, GroupLayout.PREFERRED_SIZE, 852, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 846, GroupLayout.PREFERRED_SIZE))))
                                .addContainerGap(56, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(78)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(BI_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
                                .addGap(26)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 323, GroupLayout.PREFERRED_SIZE)
                                .addGap(27)
                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 413, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(90, Short.MAX_VALUE))
        );

        JLabel label = new JLabel("基金编号：");

        JLabel lblNewLabel_1 = new JLabel("基金名称：");

        JLabel lblNewLabel_2 = new JLabel("基金类型：");

        JLabel lblNewLabel_3 = new JLabel("风险水平：");

        JLabel lblNewLabel_4 = new JLabel("基金管理：");

        JLabel lblNewLabel_5 = new JLabel("基金金额：");

        fund_idTxt = new JTextField();
        fund_idTxt.setEditable(false);
        fund_idTxt.setColumns(10);

        fund_typeTxt = new JTextField();
        fund_typeTxt.setEditable(false);
        fund_typeTxt.setColumns(10);

        fund_managerTxt = new JTextField();
        fund_managerTxt.setEditable(false);
        fund_managerTxt.setColumns(10);

        fund_nameTxt = new JTextField();
        fund_nameTxt.setEditable(false);
        fund_nameTxt.setColumns(10);

        fund_risklevelTxt = new JTextField();
        fund_risklevelTxt.setEditable(false);
        fund_risklevelTxt.setColumns(10);

        fund_amountTxt = new JTextField();
        fund_amountTxt.setEditable(false);
        fund_amountTxt.setColumns(10);

        JLabel label_1 = new JLabel("用户姓名：");

        JLabel label_2 = new JLabel("用户密码：");

        client_nameTxt = new JTextField();
        client_nameTxt.setColumns(10);

        client_passwordTxt = new JTextField();
        client_passwordTxt.setColumns(10);

        JButton button_1 = new JButton("购买 ");
        button_1.setIcon(new ImageIcon(BuyFund.class.getResource("/image/Buy.png")));
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                buyActionPerformed(e);
            }
        });

        JLabel lblNewLabel_6 = new JLabel("购买份数：");

        fundqTxt = new JTextField();
        fundqTxt.setColumns(10);
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGap(66)
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addComponent(label)
                                        .addComponent(lblNewLabel_4)
                                        .addComponent(lblNewLabel_2)
                                        .addComponent(label_1)
                                        .addComponent(lblNewLabel_6))
                                .addGap(28)
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
                                        .addComponent(fundqTxt, Alignment.TRAILING)
                                        .addComponent(client_nameTxt)
                                        .addComponent(fund_typeTxt)
                                        .addComponent(fund_managerTxt)
                                        .addComponent(fund_idTxt, GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE))
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(52)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addComponent(label_2)
                                                                .addGap(18)
                                                                .addComponent(client_passwordTxt, GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE))
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addComponent(lblNewLabel_1)
                                                                .addGap(18)
                                                                .addComponent(fund_nameTxt, GroupLayout.PREFERRED_SIZE, 175, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(lblNewLabel_3)
                                                                        .addComponent(lblNewLabel_5))
                                                                .addGap(18)
                                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(fund_amountTxt, GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                                                                        .addComponent(fund_risklevelTxt, 175, 175, 175))))
                                                .addGap(125))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(102)
                                                .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
                                                .addContainerGap())))
        );
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGap(49)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label)
                                        .addComponent(fund_idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(fund_nameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(33)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_2)
                                        .addComponent(fund_typeTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_3)
                                        .addComponent(fund_risklevelTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(33)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_4)
                                        .addComponent(lblNewLabel_5)
                                        .addComponent(fund_managerTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(fund_amountTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(35)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_1)
                                        .addComponent(label_2)
                                        .addComponent(client_nameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(client_passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(31)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_6)
                                        .addComponent(fundqTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button_1))
                                .addContainerGap(65, Short.MAX_VALUE))
        );
        panel.setLayout(gl_panel);

        fund_table = new JTable();
        fund_table.setFillsViewportHeight(true);
        fund_table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                tableMousePressed(me);
            }
        });
        fund_table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "基金编号", "基金名称", "基金类型", "风险等级", "基金管理者", "基金金额"
                }
        ) {
            boolean[] columnEditables = new boolean[]{
                    false, false, false, false, false, false
            };

            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });
        fund_table.getColumnModel().getColumn(1).setPreferredWidth(93);
        fund_table.getColumnModel().getColumn(2).setPreferredWidth(103);
        fund_table.getColumnModel().getColumn(3).setPreferredWidth(143);
        fund_table.getColumnModel().getColumn(4).setPreferredWidth(118);
        fund_table.getColumnModel().getColumn(5).setPreferredWidth(116);
        scrollPane.setViewportView(fund_table);
        getContentPane().setLayout(groupLayout);
        this.fillTable(new Fund());

    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BuyFund frame = new BuyFund();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void tableMousePressed(MouseEvent met) {
        // TODO Auto-generated method stub
        int row = this.fund_table.getSelectedRow();
        this.fund_idTxt.setText(String.valueOf(fund_table.getValueAt(row, 0)));
        this.fund_nameTxt.setText(String.valueOf(fund_table.getValueAt(row, 1)));
        this.fund_typeTxt.setText(String.valueOf(fund_table.getValueAt(row, 2)));
        this.fund_risklevelTxt.setText(String.valueOf(fund_table.getValueAt(row, 3)));
        this.fund_managerTxt.setText(String.valueOf(fund_table.getValueAt(row, 4)));
        this.fund_amountTxt.setText(String.valueOf(fund_table.getValueAt(row, 5)));
    }

    //
    private void buyActionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String fund_id = this.fund_idTxt.getText();
        String fund_amount = this.fund_amountTxt.getText();
        if (StringUtils.isEmpty(fund_id)) {
            JOptionPane.showMessageDialog(null, "请选择要购买的基金产品！");
            return;
        }
        String client_name = this.client_nameTxt.getText();
        String client_password = this.client_passwordTxt.getText();
        String fundq = this.fundqTxt.getText();

        if (StringUtils.isEmpty(client_name)) {
            JOptionPane.showMessageDialog(null, "请输入用户姓名！");
            return;
        }

        if (StringUtils.isEmpty(client_password)) {
            JOptionPane.showMessageDialog(null, "请输入用户密码！");
            return;
        }

        if (StringUtils.isEmpty(fundq)) {
            JOptionPane.showMessageDialog(null, "请输入购买份数！");
            return;
        }

        ClientDAO clientDao = new ClientDAO();
        Client client = clientDao.queryClient(new Client(client_name)).get(0);
        Integer client_id = client.getClient_id();
        String password = client.getClient_password();


        if (password.equals(client_password)) {
            Fund fund = fundDAO.queryFund(new Fund(Integer.parseInt(fund_id))).get(0);
            Buy_Fund buy_fund = buy_fundDAO.buyFund(client, fund, Integer.parseInt(fundq));
            if (buy_fund != null) {
                JOptionPane.showMessageDialog(null, "购买成功！");
                resetValue();
            } else {
                JOptionPane.showMessageDialog(null, "购买失败！");
            }
        } else {
            JOptionPane.showMessageDialog(null, "密码错误！");
            this.client_passwordTxt.setText("");
            return;
        }


    }

    private void resetValue() {
        // TODO Auto-generated method stub
        this.fund_idTxt.setText("");
        this.fund_nameTxt.setText("");
        this.fund_typeTxt.setText("");
        this.fund_risklevelTxt.setText("");
        this.fund_managerTxt.setText("");
        this.fund_amountTxt.setText("");
        this.client_nameTxt.setText("");
        this.client_passwordTxt.setText("");
        this.fundqTxt.setText("");
    }


    /**
     * 查询事件处理
     *
     * @param evt
     */
    private void queryActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String fund_name = this.BI_nameText.getText();
        Fund fund = new Fund();
        fund.setFund_name(fund_name);
        this.fillTable(fund);
    }

    /**
     * 初始化表格
     *
     * @param fund
     */
    private void fillTable(Fund fund) {
        DefaultTableModel dtm = (DefaultTableModel) fund_table.getModel();
        dtm.setRowCount(0);//清空表格
        List<Fund> fundList = fundDAO.queryFund(fund);
        for (Fund rs : fundList) {
            Vector v = new Vector();
            String up = rs.getFund_up();
            if ("1".equals(up)) {
                v.add(rs.getFund_id());
                v.add(rs.getFund_name());
                v.add(rs.getFund_type());
                v.add(rs.getFund_risklevel());
                v.add(rs.getFund_manager());
                v.add(rs.getFund_amount());
                dtm.addRow(v);
            }
        }

    }


}
